<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\auth\AdminUserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class LoginController extends Controller
{
    // http get::uri
    public function index()
    {
        Auth::logout();
        return view('auth.login');
    }

    // http get::uri/create
    public function create()
    {
        return view();
    }

    // http post::uri
    public function store(Request $request)
    {
        $account = $request->input('account');
        $password = $request->input('password');
        $admin = AdminUserModel::query()->where(['account'=>$account])->first();
        if (is_null($admin)) {
            return $this->error('账户不存在！', -1);
        }

        $hash_password = $admin->getAuthPassword();
        if (!Hash::check($password, $hash_password)) {
            return $this->error('密码不正确', -1);
        }
        if (Hash::needsRehash($hash_password)) {
            $admin->password = Hash::make($password);
            $admin->save();
        }

        Auth::login($admin);
        return $this->success($request->session()->get('auth_redirect', '/'));
    }

    // http get::uri/id
    public function show($id)
    {
        return view();
    }

    // http get::uri/id
    public function edit($id)
    {
        return view();
    }

    // http put::uri/id
    public function update(Request $request, $id)
    {
        return $this->success();
    }

    // http delete::uri/id
    public function destroy($id)
    {
        return $this->success();
    }
}
